/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "../include/string.h"

/**
 * @brief Check if a character is alphanumeric.
 *
 * @param c The character to check.
 * @return Non-zero if c is alphanumeric, zero otherwise.
 */
int Sisalnum(int c) {
    return (Sisalpha(c) || Sisdigit(c));
}  

/**
 * @brief Check if a character is alphabetic.
 *
 * @param c The character to check.
 * @return Non-zero if c is alphabetic, zero otherwise.
 */
int Sisalpha(int c) {
    return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));  
}  

/**
 * @brief Check if a character is a control character.
 *
 * @param c The character to check.
 * @return Non-zero if c is a control character, zero otherwise.
 */
int Siscntrl(int c) {
    return (c >= 0 && c <= 31) || (c == 127);
}  

/**
 * @brief Check if a character is a digit.
 *
 * @param c The character to check.
 * @return Non-zero if c is a digit, zero otherwise.
 */
int Sisdigit(int c) {
    return (c >= '0' && c <= '9');  
}  

/**
 * @brief Check if a character is graphical.
 *
 * @param c The character to check.
 * @return Non-zero if c is graphical, zero otherwise.
 */
int Sisgraph(int c) {
    return (c > 32 && c < 127);
}  

/**
 * @brief Check if a character is lowercase.
 *
 * @param c The character to check.
 * @return Non-zero if c is lowercase, zero otherwise.
 */
int Sislower(int c) {
    return (c >= 'a' && c <= 'z');  
}  

/**
 * @brief Check if a character is printable.
 *
 * @param c The character to check.
 * @return Non-zero if c is printable, zero otherwise.
 */
int Sisprint(int c) {
    return (c >= 32 && c < 127);
}  

/**
 * @brief Check if a character is punctuation.
 *
 * @param c The character to check.
 * @return Non-zero if c is punctuation, zero otherwise.
 */
int Sispunct(int c) {
    return (Sisgraph(c) && !Sisalnum(c));
}  

/**
 * @brief Check if a character is whitespace.
 *
 * @param c The character to check.
 * @return Non-zero if c is whitespace, zero otherwise.
 */
int Sisspace(int c) {
    return (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r');  
}  

/**
 * @brief Check if a character is uppercase.
 *
 * @param c The character to check.
 * @return Non-zero if c is uppercase, zero otherwise.
 */
int Sisupper(int c) {
    return (c >= 'A' && c <= 'Z');  
}  

/**
 * @brief Check if a character is a hexadecimal digit.
 *
 * @param c The character to check.
 * @return Non-zero if c is a hexadecimal digit, zero otherwise.
 */
int Sisxdigit(int c) {
    return (Sisdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'));
}  

/**
 * @brief Convert a character to lowercase.
 *
 * @param c The character to convert.
 * @return The lowercase equivalent of c, or c if it is not an uppercase letter.
 */
int Stolower(int c) {
    if (Sisupper(c))
        return c + ('a' - 'A');

    return c;
}  

/**
 * @brief Convert a character to uppercase.
 *
 * @param c The character to convert.
 * @return The uppercase equivalent of c, or c if it is not a lowercase letter.
 */
int Stoupper(int c) {
    if (Sislower(c))
        return c - ('a' - 'A');

    return c;
}
